import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_he/page/mine/xin_he_adress_management/model/xin_he_address_item_model_entity.dart';
import 'package:xin_he/page/mine/xin_he_adress_management/model/xin_he_p_c_a_model_entity.dart';

import '../../../../service/xin_he_reqest.dart';
import '../../../../utils/xin_he_event_bus.dart';
import '../../../../utils/xin_he_storage.dart';
import '../../../../utils/xin_he_toast.dart';
import '../xin_he_adress_management_logic.dart';
import 'xin_he_add_address_state.dart';

class XinHeAddAddressLogic extends GetxController {
  final XinHeAddAddressState state = XinHeAddAddressState();

  TextEditingController nameController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController detailRegionController = TextEditingController();

  XinHeAddressItemModelEntity editModel = XinHeAddressItemModelEntity();
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();


    var model = Get.arguments;
    if(model !=null){
      editModel = model;
      state.name.value = model.name??'';
      state.phone.value = model.telephone??'';
      state.region.value = '${model.province} ${model.city} ${model.area}';
      state.detailRegion.value = model.detail??'';
      state.isDefault.value = model.isDefault == 1?true:false;
      state.id.value = model.id;

      state.province.value = model.province!;
      state.provinceId.value = model.fProvinceId!;

      state.city.value = model.city!;
      state.cityId.value = model.fCityId;

      state.aera.value = model.area!;
      state.aeraId.value = model.fAreaId;

    }

    nameController = TextEditingController(text: state.name.value);
    phoneController = TextEditingController(text: state.phone.value);
    detailRegionController = TextEditingController(text: state.detailRegion.value);

    requestAddresses('');
  }

  requestAddresses(String type) async{

    var userId =  await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic>map = {
      // 'orderStatus':type,
      'userId':userId
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/zone/findTreeZone',isPost: false);

    if(!result.result || result.data['code'] != 0){
      XinHeToast.center(result.data['message']);
      return ;
    }
    List list = result.data['data'] ??[];
    List <XinHePCAModelEntity>items = [];
    if(list.isNotEmpty){
      for(var e in list){
        XinHePCAModelEntity model = XinHePCAModelEntity.fromJson(e);
        items.add(model);
      }
      state.list.value = items;
    }else {
      state.list.clear();
    }
  }


  saveAddress()async {

    if(state.name.value.isEmpty){
      XinHeToast.center('收货人姓名不能为空');
      return;
    }
    if(state.phone.value.isEmpty){
      XinHeToast.center('手机号码不能为空');
      return;
    }
    if(state.phone.value.length != 11){
      XinHeToast.center('手机号码格式不正确');
      return;
    }
    if(state.province.value.isEmpty){
      XinHeToast.center('省市区不能为空');
      return;
    }
    if(state.detailRegion.value.isEmpty){
      XinHeToast.center('详细地址不能为空');
      return;
    }


    var userId =  await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic>map = {
      // 'orderStatus':type,
      'userId': userId,
      'area':state.aera.value,
      'areaId':state.aeraId.value,
      'city':state.city.value,
      'cityId':state.cityId.value,
      'detail':state.detailRegion.value,
      'isDefault':state.isDefault.value == true?1:0,
      'name':state.name.value,
      // 'id':state.id.value,
      'province':state.province.value,
      'provinceId':state.provinceId.value,
      'telephone':state.phone.value,

    };

    if((editModel.name??'').isNotEmpty){
      map.addAll({'id':state.id.value});
    }
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/address/saveAddress',params: map,isPost: true,isPostAndwwwForm: true);

    if(!result.result || result.data['code'] != 0){
      XinHeToast.center(result.data['message']??'');
      return ;
    }

    XinHeToast.center((editModel.name??'').isNotEmpty?"地址更新成功":'地址添加成功');

    final controllerLogic = Get.find<XinHeAdressManagementLogic>();
    controllerLogic.refreshData();
    if( state.isDefault.value == true) {
      bus.emit(XinHeStorage.settingDefaultAddress);
    }
    Get.back();


  }

}
